package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Product;
import com.ruoyi.system.domain.vo.ProductVo;
import com.ruoyi.system.mapper.ProductMapper;
import com.ruoyi.system.service.CategoryService;
import com.ruoyi.system.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper,Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增产品
     * @param product
     * @return
     */
    @Override
    public int createProduct(Product product) {
        // 根据categoryName查询categoryId
        String categoryName = product.getCategoryName();
        Integer categoryId = categoryService.getCategoryIdByCategoryName(categoryName);
        if (categoryId == null) {
            throw new RuntimeException("Category not found");
        }
        product.setCategoryId(categoryId); // 设置categoryId到product对象中
        product.setCreateTime(DateUtils.getNowDate());
        Product saveProduct = saveProduct(product);
        int rows = productMapper.create(saveProduct);
        return rows;
    }

    /**
     * 删除产品 （只修改删除状态）
     * @param product
     * @return
     */
    @Override
    public int deleteProduct(Product product) {
        product.setRemoveStatus("1");
        product.setRemoveTime(DateUtils.getNowDate());
        int rows = productMapper.deleteProduct(product);
        return rows;
    }

    /**
     * 修改产品信息
     * @param product
     * @return
     */
    @Override
    public int updateProduct(Product product) {
        product.setUpdateTime(DateUtils.getNowDate());
        String categoryName = product.getCategoryName();
        Integer categoryIdByCategoryName = categoryService.getCategoryIdByCategoryName(categoryName);
        product.setCategoryId(categoryIdByCategoryName);
//        List<Integer> categoryIds = categoryService.getCategoryIds();
//        //检查获取到的categoryId是否在列表内
//        if (!categoryIds.contains(categoryId)){
//            return 0;
//        }
        Product list = saveProduct(product);
        int rows = productMapper.updateProduct(list);
        return rows;
    }

    /**
     * 下架商品信息
     * @param product
     * @return
     */
    @Override
    public int shelfProduct(Product product) {
        product.setStatus("1");
        int rows = productMapper.shelfProduct(product);
        return rows;
    }

    /**
     * 搜索产品
     * @param product
     * @return
     */
    @Override
    public List<Product> search(Product product) {
        List<Product> products = productMapper.selectProductList(product);
        return products;
    }

    /**
     * 分页查询产品列表
     * @param product
     * @return
     */
    @Override
    public List<Product> getProductList(Product product) {
        List<Product> productList = productMapper.getProductList(product);
        return productList;
    }

    /**
     * 获取产品id
     * @param id
     * @return
     */
    @Override
    public Product getProductId(Integer id) {
        Product product = productMapper.selectProductId(id);
        return product;
    }

    @Override
    public List<ProductVo> getProduct(ProductVo productVo) {
        List<ProductVo> productVo1 = productMapper.getProductVo(productVo);
        return productVo1;
    }


    public static Product saveProduct(Product product){
        product.setCategoryId(product.getCategoryId());
        product.setPrice(product.getPrice());
        product.setSummary(product.getSummary());
        product.setProductName(product.getProductName());
        product.setProductImg(product.getProductImg());
        product.setBrand(product.getBrand());
        product.setSpecification(product.getSpecification());
        product.setApplicationMethod(product.getApplicationMethod());
        product.setProperty(product.getProperty());
        product.setExpirationDate(product.getExpirationDate());
        product.setSuitableObject(product.getSuitableObject());
        product.setDosage(product.getDosage());
        product.setVirulence(product.getVirulence());
        product.setCreateTime(product.getCreateTime());
        return product;
    }

}
